#!/bin/bash
###########################################################################################
## Copyright 2003, 2015 IBM Corp                                                          ##
##                                                                                        ##
## Redistribution and use in source and binary forms, with or without modification,       ##
## are permitted provided that the following conditions are met:                          ##
##	1.Redistributions of source code must retain the above copyright notice,          ##
##        this list of conditions and the following disclaimer.                           ##
##	2.Redistributions in binary form must reproduce the above copyright notice, this  ##
##        list of conditions and the following disclaimer in the documentation and/or     ##
##        other materials provided with the distribution.                                 ##
##                                                                                        ##
## THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS AND ANY EXPRESS       ##
## OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        ##
## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ##
## THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,    ##
## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF     ##
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ##
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,  ##
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS  ##
## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           ##
############################################################################################
## File :	xorg-x11-font-utils.sh
##
## Description:	Testcase to test xorg-x11-font-utils package.
##
## Author:	Nilesh Borate, nilesh.borate@in.ibm.com
###########################################################################################
## source the standard utility functions

#cd `dirname $0`
#LTPBIN=${PWD%%/testcases/*}/testcases/bin
source $LTPBIN/tc_utils.source


################################################################################
# the testcase functions
################################################################################

#
# local setup function
#
function tc_local_setup()
{
	tc_exec_or_break "grep file"
	# Delete temporary files generated from previous test invocation, if the test does not cleanly exit.
	rm -rf fonts.scale fonts.dir myfont.ttf mynew.pcf sample_ISO10646-iso8859-1.bdf
}

#
# local cleanup function
#
function tc_local_cleanup()
{
	# Delete temporary files generated by the tests
	rm -rf fonts.scale fonts.dir myfont.ttf mynew.pcf sample_ISO10646-iso8859-1.bdf
}

#
# test01        installation check
#
function test01()
{
        tc_register     "installation check"
        tc_executes mkfontscale mkfontdir fonttosfnt ucs2any bdftopcf
        tc_pass_or_fail $? "xorg-x11-font-utils not properly installed"
}

#
# test02	mkfontscale
#
function test02()
{
	tc_register	"mkfontscale test"
	mkfontscale > $stdout 2>$stderr
	tc_fail_if_bad $? "mkfontscale command failed" || return
	[ -f ./fonts.scale ] 
	tc_fail_if_bad $? "fonts.scale file not generated" || return
	grep -q "cursor-medium-r-normal--0-0-0-0-p-0" ./fonts.scale
	tc_pass_or_fail $? "Expected cursor-medium-r-normal--0-0-0-0-p-0 in fonts.scale" \
	"--- fonts.scale ---" \
	"$(< ./fonts.scale)" 
}

#
# test03	mkfontdir
#
function test03()
{
	tc_register	"mkfontdir test"
	mkfontdir > $stdout 2>$stderr
	tc_fail_if_bad $? "mkfontdir command failed" || return
	[ -f ./fonts.dir ] 
	tc_fail_if_bad $? "fonts.dir file not generated" || return
	grep -q "4x6\.pcf" ./fonts.dir &&
	grep -q "cursor\.pfa" ./fonts.dir &&
	grep -q "sample\.bdf" ./fonts.dir
	tc_pass_or_fail $? "Expected 4x6.pcf, cursor.pfa & sample.bdf font entries in fonts.dir" \
	"--- fonts.dir ---" \
	"$(< ./fonts.dir)"
}

#
# test04	fonttosfnt
#
function test04()
{
	tc_register	"fonttosfnt test"
	# following command writes to stderr, although the command itself executes successfully and works properly.
	fonttosfnt -v -o ./myfont.ttf ./4x6.pcf &>$stdout
	tc_fail_if_bad $? "fonttosfnt command failed" || return
	[ -f ./myfont.ttf ]
	tc_fail_if_bad $? "myfont.ttf file not generated" || return
	file ./myfont.ttf 2>$stderr | grep -qi truetype 
	tc_pass_or_fail $? "Unable to generate TrueType font data file"
}

#
# test05	ucs2any
#
function test05()
{
	tc_register 	"ucs2any test"
	# following command writes to stderr, although the command itself executes successfully and works properly.
	ucs2any  sample_ISO10646.bdf ./8859-1.a0-ff.16.gz iso8859-1 &>$stdout
	tc_fail_if_bad $? "ucs2any command failed" || return
	[ -f ./sample_ISO10646-iso8859-1.bdf ]
	tc_fail_if_bad $? "sample_ISO10646-iso8859-1.bdf not generated" || return
	grep -q -i "iso8859-1" sample_ISO10646-iso8859-1.bdf
	tc_pass_or_fail $? "Expected iso8859-1 in generated file sample_ISO10646-iso8859-1.bdf"
}

#
# test06	bdftopcf
#
function test06()
{
	tc_register	"bdftopcf test"
	bdftopcf -o ./mynew.pcf sample.bdf > $stdout 2>$stderr
	tc_fail_if_bad $? "bdftopcf command failed" || return
	[ -f ./mynew.pcf ] 
	tc_fail_if_bad $? "mynew.pcf file not generated" || return
	file ./mynew.pcf 2>$stderr | grep -qi "Portable Compiled Font"
	tc_pass_or_fail $? "Unable to generate Portable Compiled Font data file"
}

################################################################################
# main
################################################################################

cd ${LTPBIN%/shared}/xorg_x11_font_utils/

TST_TOTAL=6

# standard tc_setup
tc_setup

test01 
test02
test03
test04
test05
test06
